﻿package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	public class OrigBmp extends Bitmap
	{
		private var _load:Loader;
		private var _url:URLRequest;
		public static const VALUE:String = "value";	
		
		public function OrigBmp(pixelSnapping:String="auto", smoothing:Boolean=false)
		{
			this.pixelSnapping=pixelSnapping;
			this.smoothing=smoothing;
		}
		/**
		 * set the image path to load
		 */
		public function set path(imageUrl:String):void
		{
			dispose();
			
			_url=new URLRequest(imageUrl);
			_load=new Loader();
			_load.load(_url);
			_load.contentLoaderInfo.addEventListener(Event.COMPLETE,loadedHandle);
			_load.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandle);
		}
		/**
		 * _load loaded the image complete
		 */
		private function loadedHandle(event:Event):void
		{
			_load.contentLoaderInfo.removeEventListener(Event.COMPLETE,loadedHandle);
			var bitmap:Bitmap=_load.content as Bitmap;
			this.bitmapData=bitmap.bitmapData.clone();
			bitmap.bitmapData.dispose();
			
			this.dispatchEvent(new Event(Event.COMPLETE));
			this.addEventListener(Event.REMOVED_FROM_STAGE,dispose);
		}
		private function ioErrorHandle(event:IOErrorEvent):void
		{
			trace("err");
			aacc();
			_load.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandle);
			//this.dispatchEvent(new Event(OrigBmp.VALUE));
			//this.addEventListener(Event.REMOVED_FROM_STAGE,dispose);
		}
		private function aacc():void
		{
			trace("errrorrrrrrrr")
		}
		private function dispose(event:Event=null):void
		{
			if(_url)
			{
				this.bitmapData.dispose();
				//_load.unloadAndStop(true);
				_url=null;
			}
		}
	}
}